package com.ilxqx.framework.exception;

import com.ilxqx.framework.util.ResultUtils;
import com.ilxqx.framework.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * 框架错误处理类
 * @author venus
 */
@ControllerAdvice
@Slf4j
public class FrameworkExceptionHandler extends ResponseEntityExceptionHandler {

    /**
     * 捕获框架产生的逻辑异常
     * @param throwable 异常（万恶之源）
     * @return 响应实体
     */
    @ExceptionHandler(FrameworkException.class)
    public ResponseEntity<ResultVO> handleFrameworkException(Throwable throwable) {
        log.warn("出现业务异常[{}]", throwable.getMessage());
        return new ResponseEntity<>(ResultUtils.failure(throwable.getMessage()), HttpStatus.OK);
    }

    /**
     * 捕获整个系统出现的其他异常
     * @param throwable 万恶之源
     * @return 响应实体
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResultVO> handleException(Throwable throwable) {
        log.error("系统内部出现异常[{}]，具体异常信息：{}", throwable.getMessage(), throwable);
        return new ResponseEntity<>(ResultUtils.failure("很抱歉，服务器内部出现异常"), HttpStatus.INTERNAL_SERVER_ERROR);
    }

}
